package com.yztc.chunboapp;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ViewFlipper;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.Toast;

import com.yztc.ui.classify.ClassifyFragment;
import com.yztc.ui.cook.CookFragment;
import com.yztc.ui.home.HomeFragment;
import com.yztc.ui.my.MyFragment;
import com.yztc.ui.shopping.ShoppingFragment;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.sharesdk.framework.ShareSDK;


public class MainActivity extends AppCompatActivity {
     /**
    * 登录状态标识
    * */
    public static boolean tag = false;
    //当前登录用户名
    public static String currentUserName;
    private ViewFlipper filpper;
    private static final int HOME_PAGE = 1;
    public static final int CLASSIFY_PAGE = 2;
    public static final int COOK_PAGE = 3;
    public static final int SHOPPING_PAGE = 4;
    public static final int MY_PAGE = 5;
    public static HomeFragment mHomeFragment;
    private ClassifyFragment mClassifyFragment;
    private CookFragment mCookFragment;
    private ShoppingFragment mShoppingFragment;
    private MyFragment mMyFragment;
    private FragmentManager manager;
    public static FragmentTransaction transaction;
    private RadioGroup mRadioGroup;
    private RadioButton mRadioButton;
    private ImageView mRunImg;

//    private Handler handler=new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            switch (msg.what){
//                case 1:
//                    filpper.setDisplayedChild(1);
//                    break;
//            }
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //init();
        initData();
        setListener();
        //默认初始化Bmob
        Bmob.initialize(this,"5b443a6edb126b82946b44fa3cd54e67");
        //初始化shareSDK
        ShareSDK.initSDK(this);
    }

//    private void init() {
//        //获取“缩放”动画资源
//        final Animation mScale= AnimationUtils.loadAnimation(this, R.anim.anim_scale);
//        mRunImg.startAnimation(mScale);
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                handler.sendEmptyMessage(1);
//            }
//        },3000);
//    }

    @Override
    protected void onResume() {
        super.onResume();
        //获取当前用户信息
        BmobUser bmobUser = BmobUser.getCurrentUser();
        if(bmobUser != null){
            tag = true;
            currentUserName = bmobUser.getUsername();
        }
    }


    private void initView() {
        mRadioGroup = (RadioGroup) findViewById(R.id.rg_guide);
        mRadioButton = (RadioButton) findViewById(R.id.rb_home);
        //mRunImg= (ImageView) findViewById(R.id.img_run);
        filpper= (ViewFlipper) findViewById(R.id.fliper);
    }
    private void initData() {
        manager = getSupportFragmentManager();
        //默认下导航第一个radioButton被选中
        mRadioButton.setChecked(true);
        //默认下导航第一个radioButton对应的fragment被加载
        transaction = manager.beginTransaction();
        mHomeFragment = new HomeFragment();
        transaction.add(R.id.line_guide,mHomeFragment);
        transaction.commit();
    }
    private void setListener() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_home:
                        selectFragment(HOME_PAGE);
                        break;
                    case R.id.rb_classify:
                        selectFragment(CLASSIFY_PAGE);
                        break;
                    case R.id.rb_cook:
                        selectFragment(COOK_PAGE);
                        break;
                    case R.id.rb_shopping:
                        selectFragment(SHOPPING_PAGE);
                        break;
                    case R.id.rb_my:
                        selectFragment(MY_PAGE);
                        break;
                }
            }
        });
    }

    public void hideFragment(FragmentTransaction transaction){
        if (mHomeFragment != null){
            transaction.hide(mHomeFragment);
        }
        if (mClassifyFragment != null){
            transaction.hide(mClassifyFragment);
        }
        if (mCookFragment != null){
            transaction.hide(mCookFragment);
        }
        if (mShoppingFragment != null){
            transaction.hide(mShoppingFragment);
        }
        if (mMyFragment != null){
            transaction.hide(mMyFragment);
        }
    }

    public void selectFragment(int constant){
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        switch (constant){
            case HOME_PAGE:
                if (mHomeFragment == null){
                    mHomeFragment = new HomeFragment();
                    transaction.add(R.id.line_guide,mHomeFragment);
                }else {
                    transaction.show(mHomeFragment);
                }
                break;
            case CLASSIFY_PAGE:
                if (mClassifyFragment == null){
                    mClassifyFragment = new ClassifyFragment();
                    transaction.add(R.id.line_guide,mClassifyFragment);
                }else {
                    transaction.show(mClassifyFragment);
                }
                break;
            case COOK_PAGE:
                if (mCookFragment == null){
                    mCookFragment = new CookFragment();
                    transaction.add(R.id.line_guide,mCookFragment);
                }else {
                    transaction.show(mCookFragment);
                }
                break;
            case SHOPPING_PAGE:
                if (mShoppingFragment == null){
                    mShoppingFragment = new ShoppingFragment();
                    transaction.add(R.id.line_guide,mShoppingFragment);
                }else {
                    transaction.show(mShoppingFragment);
                }
                break;
            case MY_PAGE:
                if (mMyFragment == null){
                    mMyFragment = new MyFragment();
                    transaction.add(R.id.line_guide,mMyFragment);
                }else {
                    transaction.show(mMyFragment);
                }
                break;
        }
        transaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁sdk
        ShareSDK.stopSDK(this);
    }

    //再按一次退出程序
    private long time = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis()-time)>2000) {
                Toast toast = Toast.makeText(MainActivity.this,"再按一次退出程序", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER,0,0);
                toast.show();
                time = System.currentTimeMillis();

            }else {
                finish();
            }
        }
        return false;
    }
}
